package com.wutuo.aggragation.rollcall.dao;

import cn.apestar.core.database.BaseMapper;
import com.wutuo.aggragation.rollcall.domain.Task;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

public interface TaskMapper extends BaseMapper {

    @Delete({ "delete from task", "where id = #{id,jdbcType=BIGINT}" })
    int deleteByPrimaryKey(Long id);

    @Insert({ "insert into task (id, task_name, ", "task_time, create_by, ", "create_date, update_by, ",
        "update_date, remarks, ", "del_flag,institution_id,task_type)",
        "values (#{id,jdbcType=BIGINT}, #{taskName,jdbcType=VARCHAR}, ",
        "#{taskTime,jdbcType=TIMESTAMP}, #{createBy,jdbcType=BIGINT}, ",
        "#{createDate,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=BIGINT}, ",
        "#{updateDate,jdbcType=TIMESTAMP}, #{remarks,jdbcType=VARCHAR}, ",
        "#{delFlag,jdbcType=CHAR},#{institutionId,jdbcType=CHAR} , #{taskType,jdbcType=VARCHAR})" })
    @SelectKey(before = false, keyProperty = "id", resultType = Long.class, statement = "SELECT LAST_INSERT_ID() AS id")
    int insert(Task record);

    @InsertProvider(type = TaskSqlProvider.class, method = "insertSelective")
    @SelectKey(before = false, keyProperty = "id", resultType = Long.class, statement = "SELECT LAST_INSERT_ID() AS id")
    int insertSelective(Task record);

    @Select({ "select", "id, task_name, task_time,task_type, create_by, create_date, update_by, update_date, remarks, ",
        "del_flag,institution_id", "from task", "where id = #{id,jdbcType=BIGINT} and del_flag = 1" })
    @Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
        @Result(column = "task_name", property = "taskName", jdbcType = JdbcType.VARCHAR),
        @Result(column = "task_time", property = "taskTime", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "task_type", property = "taskType", jdbcType = JdbcType.VARCHAR),
        @Result(column = "create_by", property = "createBy", jdbcType = JdbcType.BIGINT),
        @Result(column = "create_date", property = "createDate", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "update_by", property = "updateBy", jdbcType = JdbcType.BIGINT),
        @Result(column = "update_date", property = "updateDate", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "remarks", property = "remarks", jdbcType = JdbcType.VARCHAR),
        @Result(column = "del_flag", property = "delFlag", jdbcType = JdbcType.CHAR) })
    Task selectByPrimaryKey(Long id);

    @Select({ "select", "id, task_name, task_time,task_type, create_by, create_date, update_by, update_date, remarks, ",
        "del_flag", "from task", "where institution_id = #{intitutionId,jdbcType=BIGINT} ",
        "and task_name = #{taskName,jdbcType=VARCHAR} ",
        "and date_format(task_time,'%Y-%m-%d') = date_format(#{taskTime,jdbcType=TIMESTAMP}, '%Y-%m-%d') ",
        " and del_flag = 1" })
    @Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
        @Result(column = "task_name", property = "taskName", jdbcType = JdbcType.VARCHAR),
        @Result(column = "task_time", property = "taskTime", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "task_type", property = "taskType", jdbcType = JdbcType.VARCHAR),
        @Result(column = "create_by", property = "createBy", jdbcType = JdbcType.BIGINT),
        @Result(column = "create_date", property = "createDate", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "update_by", property = "updateBy", jdbcType = JdbcType.BIGINT),
        @Result(column = "update_date", property = "updateDate", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "remarks", property = "remarks", jdbcType = JdbcType.VARCHAR),
        @Result(column = "del_flag", property = "delFlag", jdbcType = JdbcType.CHAR) })
    Task selectByIntitutionAndName(@Param("intitutionId") Long intitutionId, @Param("taskName") String taskName,
                                   @Param("taskTime") Date taskTime);

    @SelectProvider(type = TaskSqlProvider.class, method = "selectByObject")
    @Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
        @Result(column = "task_name", property = "taskName", jdbcType = JdbcType.VARCHAR),
        @Result(column = "task_time", property = "taskTime", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "task_type", property = "taskType", jdbcType = JdbcType.VARCHAR),
        @Result(column = "create_by", property = "createBy", jdbcType = JdbcType.BIGINT),
        @Result(column = "create_date", property = "createDate", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "update_by", property = "updateBy", jdbcType = JdbcType.BIGINT),
        @Result(column = "update_date", property = "updateDate", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "institution_id", property = "institutionId", jdbcType = JdbcType.BIGINT),
        @Result(column = "remarks", property = "remarks", jdbcType = JdbcType.VARCHAR),
        @Result(column = "del_flag", property = "delFlag", jdbcType = JdbcType.CHAR) })
    List<Task> selectByObject(@Param("task") Task record);

    @SelectProvider(type = TaskSqlProvider.class, method = "selectByInstitutionId")
    @Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
        @Result(column = "task_name", property = "taskName", jdbcType = JdbcType.VARCHAR),
        @Result(column = "task_time", property = "taskTime", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "task_type", property = "taskType", jdbcType = JdbcType.VARCHAR),
        @Result(column = "create_by", property = "createBy", jdbcType = JdbcType.BIGINT),
        @Result(column = "create_date", property = "createDate", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "update_by", property = "updateBy", jdbcType = JdbcType.BIGINT),
        @Result(column = "update_date", property = "updateDate", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "institution_id", property = "institutionId", jdbcType = JdbcType.BIGINT),
        @Result(column = "remarks", property = "remarks", jdbcType = JdbcType.VARCHAR),
        @Result(column = "del_flag", property = "delFlag", jdbcType = JdbcType.CHAR) })
    List<Task> selectByInstitutionId(@Param("institutionId") Long institutionId, @Param("index") int index,
                                     @Param("size") int size);

    @UpdateProvider(type = TaskSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Task record);

    @Update({ "update task", "set task_name = #{taskName,jdbcType=VARCHAR},",
        "task_time = #{taskTime,jdbcType=TIMESTAMP},", "task_type = #{taskType,jdbcType=VARCHAR},",
        "create_by = #{createBy,jdbcType=BIGINT},", "create_date = #{createDate,jdbcType=TIMESTAMP},",
        "update_by = #{updateBy,jdbcType=BIGINT},", "update_date = #{updateDate,jdbcType=TIMESTAMP},",
        "remarks = #{remarks,jdbcType=VARCHAR},", "del_flag = #{delFlag,jdbcType=CHAR}",
        "institution_id = #{institutionId,jdbcType=CHAR}", "where id = #{id,jdbcType=BIGINT}" })
    int updateByPrimaryKey(Task record);
}